package com.codeoss.framework.workflow.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.workflow.context.WorkflowContext;
import com.codeoss.framework.workflow.def.NodeDef;
import com.codeoss.module.workflow.model.WorkflowActivity;
import com.codeoss.module.workflow.model.WorkflowActivityInstance;
import com.codeoss.module.workflow.model.WorkflowToken;
import com.codeoss.module.workflow.service.IWorkflowActivityInstanceService;
import com.codeoss.module.workflow.service.IWorkflowActivityService;
import com.codeoss.module.workflow.service.IWorkflowTokenService;

@Service
public class JoinNodeService extends AbstractNodeService {
	@Autowired
	private IWorkflowTokenService workflowTokenService;
	@Autowired
	private IWorkflowActivityInstanceService workflowActivityInstanceService;
	@Autowired
	private IWorkflowActivityService workflowActivityService;
	@Override
	@Transactional
	public void execute(WorkflowContext workflowContext) {
		WorkflowToken workflowToken=workflowContext.getWorkflowToken();
		Long parentTokenId=workflowToken.getParentTokenId();
		QueryWrapper<WorkflowToken> tokenQuery=new QueryWrapper<WorkflowToken>();
		tokenQuery.eq("parent_token_id", parentTokenId);
		List<WorkflowToken> tokenList=workflowTokenService.list(tokenQuery);
		NodeDef nodeDef=workflowContext.getNodeDef();
		int count=0;
		for (int i=0;i<tokenList.size();i++) {
			WorkflowToken subToken=tokenList.get(i);
			Long workflowActivityInstanceId=subToken.getWorkflowActivityInstanceId();
			WorkflowActivityInstance workflowActivityInstance=workflowActivityInstanceService.getById(workflowActivityInstanceId);
			WorkflowActivity workflowActivity=workflowActivityService.getById(workflowActivityInstance.getWorkflowActivityId());
			if(workflowActivity.getNodeId().equals(nodeDef.getId())) {
				count++;
			}
		}
		if(tokenList.size()==count) {
			for (int i=0;i<tokenList.size();i++) {
				WorkflowToken subToken=tokenList.get(i);
				Long workflowActivityInstanceId=subToken.getWorkflowActivityInstanceId();
				WorkflowActivityInstance workflowActivityInstance=workflowActivityInstanceService.getById(workflowActivityInstanceId);
				workflowActivityInstance.setEndDate(new Timestamp(System.currentTimeMillis()));
				workflowActivityInstance.setState("CO");
				workflowActivityInstanceService.update(workflowActivityInstance);
			}
			
			WorkflowContext context=new WorkflowContext();
			context.setWorkflowActivity(workflowContext.getWorkflowActivity());
			context.setWorkflowActivityInstance(workflowContext.getWorkflowActivityInstance());
			context.setWorkflowProcess(workflowContext.getWorkflowProcess());
			context.setNodeDef(nodeDef);
			
			QueryWrapper<WorkflowToken> parentTokenQuery=new QueryWrapper<WorkflowToken>();
			parentTokenQuery.eq("id", parentTokenId);
			WorkflowToken parentToken=workflowTokenService.getOne(parentTokenQuery);
			context.setWorkflowToken(parentToken);
			end(context);
		}
	}
}
